package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *  根据路径Request URL: http://localhost:8080/setmeal
     *  json
     * method:post
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        //查看一下
        log.info("setmealDto:{}",setmealDto);
       //调用service的savewithdish方法
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
     * 批量修改状态(停售/启售)
     * 根据Request URL: http://localhost:8080/setmeal/status/0?ids=1534057078404956162
     * @param status
     * @param ids
     * method:post
     */
    @PostMapping("/status/{status}")
    public R<String> updataStatus(@PathVariable int status,@RequestParam("ids") List<Long> ids ){
        //查看
        log.info("ids:{}",ids);
        //service修改状态方法
        setmealService.updataStatus(status,ids);
        return R.success("修改状态成功");
    }
    /**
     * 根据Request URL: http://localhost:8080/setmeal?ids=1415580119015145474,153405707840
     * method:delete
     */
@DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
    //查看
    log.info("ids:{}",ids);
    //service调用removewithdish删除
    setmealService.removeWithDish(ids);
    return R.success("删除套餐成功");
}
/**
 * 分页条件查询
 * Request URL: http://localhost:8080/setmeal/page?page=1&pageSize=10&name=%E6%97%B6
 * method:get
 * @param page
 * @param pageSize
 * @param name
 * 模糊查询like
 * lambda构造条件
 */
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name) {
    //分页构造器对象
    //套餐
    Page<Setmeal> pageInfo = new Page<>(page, pageSize);
    //dto关联
    Page<SetmealDto> dtoPage = new Page<>();

    LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

    //添加查询条件使用like模糊查询name属性
    lqw.like(name != null, Setmeal::getName, name);
    //添加排序条件，根据更新时间降序排列
    lqw.orderByDesc(Setmeal::getUpdateTime);

    setmealService.page(pageInfo, lqw);

    //对象拷贝 除"records"
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<Setmeal> records = pageInfo.getRecords();

    List<SetmealDto> list = records.stream().map(item -> {
        SetmealDto setmealDto = new SetmealDto();
        //对象拷贝
        BeanUtils.copyProperties(item, setmealDto);
        //分类id
        Long categoryId = item.getCategoryId();
        //根据分类id查询分类对象
        Category category = categoryService.getById(categoryId);
        if (category != null) {
            //分类名称
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
        }
        return setmealDto;
    }).collect(Collectors.toList());
    dtoPage.setRecords(list);
    return R.success(dtoPage);

}

    /**
     * 修改套餐
     * Request URL: http://localhost:8080/setmeal
     * method:put
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String>updata(@RequestBody SetmealDto setmealDto){
        //查看,转成字符串
        log.info(setmealDto.toString());
        //service调用updatawithsetmealname修改
        setmealService.updateWithSetmealName(setmealDto);
        return R.success("修改套餐成功");

}
    /**
     * 根据id查询套餐信息
     *修改套餐回显套餐详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithSetmealName(id);

        return R.success(setmealDto);
    }
    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);


        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
